package net.wanho.ioc;

import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

/**
 * Author：汤小洋
 * Date：2024-08-08 16:45
 * Description：<描述>
 */
// 默认是singleton单例，配置为prototype非单例
@Scope("prototype")
// 默认是预实例化，配置为懒初始化
// @Lazy
@Component
// @RequiredArgsConstructor
public class SpringBean {

    @Value("${name}")
    private String name;

    // 方式1：基于属性注入
    // @Resource
    // @Autowired
    private final OtherBean otherBean;

    // 方式2：基于setter方法注入
    // @Autowired
    // public void setOtherBean(OtherBean otherBean){
    //     this.otherBean = otherBean;
    // }

    // 方式3：基于constructor注入
    @Autowired
    public SpringBean(OtherBean otherBean){
        System.out.println("SpringBean实例化。。。。。。。。。。");
        this.otherBean = otherBean;
    }


    // 初始化方法，执行初始化操作
    @PostConstruct
    public void init(){
        this.name = this.name.toUpperCase();
    }

    // 销毁方法，执行销毁前的操作
    @PreDestroy
    public void destroy() {
        System.out.println("SpringBean.destroy()");
    }

    public void show(){
        System.out.println(name + "===================" + otherBean);
    }
}
